﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SQLEditor.admin.entity;
using SQLEditor.admin.service;
using SQLEditor.dal;
namespace SQLEditor.admin.ashx
{
    /// <summary>
    /// rm_menu 的摘要说明
    /// </summary>
    public class rm_menu : rm_base
    {
        private MenuService s = new MenuService();

        public override void getall(HttpContext context)
        {
            string json2 = s.getall();
            context.Response.Write(json2);
        }

        public override void search(HttpContext context)
        {
            string strWhere = "1=1";
            string sort = context.Request.Params["sort"] ?? "id";
            string order = context.Request.Params["order"] ?? "asc";
            int pageIndex = int.Parse(context.Request.Params["page"] ?? "1");
            int pageSize = int.Parse(context.Request.Params["rows"] ?? "20");

            string json = s.search(pageSize, pageIndex, sort + " " + order, strWhere);
            context.Response.Write(json);

        }

        public override void add(HttpContext context)
        {
            string name = context.Request.Params["ui_menu_name_edit"] ?? "";
            string code = context.Request.Params["ui_menu_code_edit"] ?? "";
            string icon = context.Request.Params["ui_menu_icon_edit"] ?? "";
            string linkAddress = context.Request.Params["ui_menu_linkaddress_edit"] ?? "";
            string sort = context.Request.Params["ui_menu_sort_edit"] ?? "";
            string strParentId = context.Request.Params["ui_menu_parentId_edit"];
            if (string.IsNullOrEmpty(strParentId)) strParentId = "0";
            int parentId = int.Parse(strParentId);
            if (string.IsNullOrEmpty(sort)) sort = "10";

            tbMenu b = new tbMenu
            {
                name = name,
                code = code,
                sort = int.Parse(sort),
                icon = icon,
                linkAddress = linkAddress,                
                parentId = parentId,
                addDate = DateTime.Now
            };
            string json = s.add(b);
            context.Response.Write(json);

        }
        public override void edit(HttpContext context)
        {
            int id = Convert.ToInt32(context.Request.Params["ui_menu_id_edit"] ?? "0");
            string name = context.Request.Params["ui_menu_name_edit"] ?? "";
            string code = context.Request.Params["ui_menu_code_edit"] ?? "";
            string icon = context.Request.Params["ui_menu_icon_edit"] ?? "";
            string linkAddress = context.Request.Params["ui_menu_linkaddress_edit"] ?? "";
            string sort = context.Request.Params["ui_menu_sort_edit"] ?? "";
            
            string strParentId = context.Request.Params["ui_menu_parentId_edit"];
            if (string.IsNullOrEmpty(strParentId)) strParentId = "0";
            int parentId = int.Parse(strParentId);

            if (string.IsNullOrEmpty(sort)) sort = "10";

            tbMenu b = new tbMenu
            {
                id = id,
                name = name,
                code = code,
                sort = int.Parse(sort),
                icon = icon,
                linkAddress = linkAddress,
                parentId = parentId,
                addDate = DateTime.Now
            };
            string json = s.edit(b);
            context.Response.Write(json);
        }

        public override void delete(HttpContext context)
        {
            string ids = context.Request.Params["ids"];
            string json = s.delete(ids);
            context.Response.Write(json);
        }

        public override void other(HttpContext context)
        {
            UserEntity userFromCookie = UserService.GetUser(context);   //获取cookie里的用户对象
            string action = context.Request.Params["action"];
            switch (action)
            {
                case "getUserMenu":  //获取特定用户能看到的菜单（左侧树）
                    {
                        int uid = 1;
                        if (userFromCookie != null)
                        {
                            uid = userFromCookie.id;
                        }
                        string json = s.getMenuByUserId(uid);
                        context.Response.Write(json);
                        break;
                    }
                case "getRoleMenu":
                    {
                        int roleId = Convert.ToInt32(context.Request.Params["roleId"]);
                        string json = s.getMenuByRoleId(roleId);
                        context.Response.Write(json);
                        break;
                    }
                case "setbutton":
                    {
                        int menuId = Convert.ToInt32(context.Request.Params["menuId"]);
                        string buttonIds = context.Request.Params["buttonIds"];
                        string json = s.setbutton(menuId, buttonIds);
                        context.Response.Write(json);
                        break;
                    }
                case "getMyAuthority":
                    {
                        string json = s.getMyAuthority(userFromCookie.id);
                        context.Response.Write(json);
                        break;
                    }
            }
        }
    }
}